package com.atguigu.guli.service.ucenter.controller.api;

import com.atguigu.guli.service.base.JwtHelper;
import com.atguigu.guli.service.base.JwtInfo;
import com.atguigu.guli.service.bean.eduBean.MemberBean;
import com.atguigu.guli.service.result.R;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.entity.config.FromConfig;
import com.atguigu.guli.service.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

//
@RestController
@RequestMapping("/api/ucenter/member")
public class ApiMemberController {

    @Autowired
    MemberService memberService;

    //远程调用,查询用户信息
    @GetMapping("/getUcenter/{memberId}")
    public R getUcenter(@PathVariable("memberId") String memberId){
        Member member = memberService.getById(memberId);
        MemberBean memberBean = new MemberBean();
        BeanUtils.copyProperties(member, memberBean);
        return R.ok().data("memberBean",memberBean);
    }

    // 远程调用,查询登录者数量
    @GetMapping("/getRegisterNum/{date}")
    public  R getRegisterNum(@PathVariable("date") String date){
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("Date(gmt_create)", date);
        int count = memberService.count(wrapper);
        return R.ok().data("count", count);
    }




    @ApiOperation("用户注册")
    @PostMapping("/regist") //用户注册
    public R regist(@RequestBody FromConfig fromConfig){
        try {
            memberService.regist(fromConfig);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("用户登录")
    @GetMapping("/login") //用户登录
    public R login(@RequestParam String mobile,@RequestParam String password){
        System.out.println(mobile);
     String jwt = memberService.login(mobile,password);
     return R.ok().data("jwt", jwt);
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/auth/getUserInfo") //获取的客户信息
    public R getUserInfo(HttpServletRequest request){
        //根据前端的请求头中,获取token属性里面的值,
        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        return R.ok().data("item", jwtInfo);
    }



}
